/**
 * 
 */
package com.hz.winnie.demands.service;

import java.util.List;

import com.hz.winnie.demands.common.DemandsConstants;
import com.hz.winnie.demands.dao.ProductDao;
import com.hz.winnie.demands.pojo.Demand;
import com.hz.winnie.demands.pojo.Product;

/**
 * @description 产品服务实现类
 * @author winnie
 * @date 2011-4-7
 * @version 1.0
 */
public class ProductServiceImpl implements ProductService {

	private ProductDao productDao;

	private DemandsService demandsService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hz.winnie.demands.service.ProductService#addProduct(com.hz.winnie
	 * .demands.pojo.Product)
	 */
	@Override
	public int addProduct(Product product) {
		int productId = productDao.create(product);
		return productId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hz.winnie.demands.service.ProductService#editProduct(com.hz.winnie
	 * .demands.pojo.Product)
	 */
	@Override
	public boolean editProduct(Product product) {
		productDao.update(product);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hz.winnie.demands.service.ProductService#getProductById(java.lang
	 * .String)
	 */
	@Override
	public Product getProductById(String productId) {
		Product product = productDao.getProductById(productId);
		return product;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hz.winnie.demands.service.ProductService#listAllProduct()
	 */
	@Override
	public List<Product> listAllProduct(Product product) {
		List<Product> products = productDao.find(product);
		for (Product p : products) {
			Demand demand = new Demand();
			demand.setProductId(p.getId());
			String[] statuslist = { DemandsConstants.DEM_STATUS_UN_FOLLOW,
					DemandsConstants.DEM_STATUS_FOLLOWING };
			demand.setStatuslist(statuslist);
			int count = demandsService.countDemandsByCondition(demand);
			p.setUnDoDemCount(count);
		}

		return products;
	}

	public int countAllProduct() {
		return productDao.countAllProduct();
	}

	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public DemandsService getDemandsService() {
		return demandsService;
	}

	public void setDemandsService(DemandsService demandsService) {
		this.demandsService = demandsService;
	}

	@Override
	public boolean isSameName(String name, String productId) {
		List<Product> list = productDao.getProductsByName(name, productId);
		if (list == null || list.isEmpty()) {
			return false;
		}
		return true;
	}

}
